<?php

class Guard extends Member implements PermissionProvider
{
    private static $singular_name = "Guard Profile";

    private static $plural_name = "Guard Profiles";

    private static $db = array(
        "NRIC" => "Varchar(30)",
        "ContactNumber" => "Varchar(50)",
    );

    private static $searchable_fields = array(
        "FirstName",
        "Username",
        "ContactNumber",
        "AccountStatus"
    );

    private static $summary_fields = array(
        "FirstName",
        "Username",
        "ContactNumber",
        "FormattedAccountStatus",
        "Created.Nice24"
    );

    private static $default_sort = "ID DESC";

    private static $field_labels = array(
        "NRIC" => "NRIC/Passport",
        "FormattedAccountStatus" => "Account Status",
        "Created.Nice24" => "Registered At"
    );

    public function canView($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "VIEW_Guard"
        ), "any", $member);
    }

    public function canEdit($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "EDIT_Guard"
        ), "any", $member);
    }

    public function canDelete($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "DELETE_Guard"
        ), "any", $member);
    }

    public function canCreate($member = null)
    {
        return Permission::check(array(
            "ADMIN",
            "CREATE_Guard"
        ), "any", $member);
    }

    public function FormattedAccountStatus()
    {
        return MemberExtension::GetFormattedAccountStatus($this->AccountStatus);
    }

    public function getDefaultSearchContext()
    {
        $context = parent::getDefaultSearchContext();

        $context->removeFieldByName("AccountStatus");
        $context->addField(MemberExtension::GetAccountStatusField(true));

        return $context;
    }

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $fields->insertAfter(new TextField("Username", "Username"), "Email");

        $fields->insertAfter(new TextField("Created", "Registered At"), "LastVisited");
        $fields->makeFieldReadonly("Created");

        $fields->replaceField("Gender", MemberExtension::GetGenderField());

        $fields->replaceField("AccountStatus", MemberExtension::GetAccountStatusField());

        $fields->removeByName(array(
            "Locale",
            "DirectGroups",
            "Permissions",
            "DateFormat",
            "TimeFormat",
            "SnPUpload"
        ));

        return $fields;
    }

    public function onAfterWrite()
    {
        $mem = Member::get()->byID($this->ID);

        if (!$mem->inGroup("guard")) {
            $mem->addToGroupByCode("guard", "Guard");
        }

        parent::onAfterWrite();
    }

    public function providePermissions()
    {
        $category = "Manage guard permissions";

        return array(
            "VIEW_Guard" => array(
                "name" => "View all records",
                "category" => $category
            ),
            "CREATE_Guard" => array(
                "name" => "Create new record",
                "category" => $category
            ),
            "EDIT_Guard" => array(
                "name" => "Edit record",
                "category" => $category
            ),
            "DELETE_Guard" => array(
                "name" => "Delete record",
                "category" => $category
            )
        );
    }
}